package com.morder.provider;

import org.json.JSONException;
import org.json.JSONObject;

import com.morder.util.Constant;

import android.content.Context;

public class LoginRequest extends BaseRequest {
  private static final String TAG = "LoginRequest";
  private String mNumber;
  private String mPwd;

  public LoginRequest(Context context, String number, String pwd) {
    super(context);
    this.mNumber = number;
    this.mPwd = pwd;
  }

  @Override
  protected void onParseRequest(JSONObject object) {
    try {
      Params params = new Params();
      String device = "";
      String os = "";
      params.setDevice(device);
      params.setOs(os);
      params.setUid(mNumber);
      params.setPwd(mPwd);
      object.put(Constant.PARAMS, getParamsValue(params));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  private JSONObject getParamsValue(Params params) {
    JSONObject object = new JSONObject();
    try {
      object.put(Constant.Params.DEVICE, params.getDevice());
      object.put(Constant.Params.OS, params.getOs());
      object.put(Constant.Params.UID, params.getUid());
      object.put(Constant.Params.PWD, params.getPwd());
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return object;
  }

  @Override
  protected void onParseResponse(JSONObject result) {
    JSONObject data = result.optJSONObject(Constant.DATA);
    String key = data.optString(Constant.Data.KEY);
    if ((key != null) && (key.length() > 0) && !"".equals(key)) {
      System.out.println(TAG + "key: " + key);
    }
    String uid = data.optString(Constant.Data.UID);
    if ((uid != null) && (uid.length() > 0) && !"".equals(uid)) {
      System.out.println(TAG + "uid: " + uid);
    }
    String pwd = data.optString(Constant.Data.PWD);
    if ((pwd != null) && (pwd.length() > 0) && !"".equals(pwd)) {
      System.out.println(TAG + "pwd: " + pwd);
    }
  }

}
